<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);

require_once $basedir . '/../../../../extlib/Crypt/RSA.php';
require_once $basedir . '/../../../../extlib/Crypt/Random.php';
require_once $basedir . '/../../../../extlib/Crypt/Hash.php';
require_once $basedir . '/../../../../extlib/Math/BigInteger.php';

require_once $basedir . '/../../../../classes/magic.php';
require_once $basedir . '/../../../../classes/magic/envelope.php';
require_once $basedir . '/../../../../classes/magic/signature/verify.php';

class Magic_Signature_VerifyTest extends PHPUnit_Framework_TestCase {
    
    public function testVerificationOfSignature() {
        
        $rawEnvelope = '<?xml version="1.0" encoding="UTF-8"?>
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
  <me:data type="application/atom+xml">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGVudHJ5IHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDA1L0F0b20iIHhtbG5zOmFjdGl2aXR5PSJodHRwOi8vYWN0aXZpdHlzdHJlYS5tcy9zcGVjLzEuMC8iPgogIDxpZD5taW1pbWU6MTI4MDg0MzI4MzwvaWQ-CiAgPHRpdGxlPlVzZXIgQyBpcyBub3cgZm9sbG93aW5nIHVzZXItYkBsb2NhbGhvc3Q8L3R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ThC1E7K4Cbln6MZetMXUa3IcxRPJTdEsBojNsBE7H8afpDpEd2Dyjbbar0=</me:sig>
</me:env>';
        
        $publicKey = "RSA.gGvGh83fHtavoKyqcld5oZUW0LNIwdr-zXfEXjfLY2FwuQzC-5gHNU59l-1NNKWTlEREti6I6Wn7b18NOnZNXzpjqE9yzUZoK4JB4je4WnaWdvDTapmrVQO1qaVD4zm589TQ93Q_hUnApziTtJ_0wd7IUSnDk4lmAyF7k64w52U=.AQAB";
        
        $magicEnv = Magic_Envelope::buildFromRawXML($rawEnvelope);
        
        $verifyier = new Magic_Signature_Verify($publicKey);
        $this->assertEquals(true, $verifyier->magicEnvelopeHasValidSignature($magicEnv));
    }
    
    public function testVerificationOfSignatureIdenticaFails() {
        
        $rawEnvelope = '<?xml version="1.0" encoding="UTF-8"?>
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
  <me:data type="application/atom+xml">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGVudHJ5IHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDA1L0F0b20iIHhtbG5zOmFjdGl2aXR5PSJodHRwOi8vYWN0aXZpdHlzdHJlYS5tcy9zcGVjLzEuMC8iPgogIDxpZD5taW1pbWU6MTI4MTA5NDk3OTwvaWQ-CiAgPHRpdGxlPkB0a29za2ksIEFyZSB5b3UgZ2V0dGluZyB0aGlzIHNhbG1vbiBuaWNlbHk_PC90aXRsZT4KICA8Y29udGVudCB0eXBlPSJodG1sIj5AdGtvc2tpLCBBcmUgeW91IGdldHRpbmcgdGhpcyBzYWxtb24gbmljZWx5PzwvY29udGVudD4KICA8YXV0aG9yPgogICAgPHVyaT5hY2N0Omtvc2tpQGxvYnN0ZXJtb25zdGVyLm9yZzwvdXJpPgogICAgPG5hbWU-VHVvbWFzIEtvc2tpPC9uYW1lPgogIDwvYXV0aG9yPgogIDxhY3Rpdml0eTphY3Rvcj4KICAgIDxhY3Rpdml0eTpvYmplY3QtdHlwZT5odHRwOi8vYWN0aXZpdHlzdHJlYS5tcy9zY2hlbWEvMS4wL3BlcnNvbjwvYWN0aXZpdHk6b2JqZWN0LXR5cGU-CiAgICA8aWQ-aHR0cDovL3d3dy5sb2JzdGVybW9uc3Rlci5vcmcvcHJvZmlsZS9rb3NraTwvaWQ-CiAgICA8dGl0bGU-VHVvbWFzIEtvc2tpPC90aXRsZT4KICAgIDxsaW5rIHJlbD0iYWx0ZXJuYXRlIiB0eXBlPSJ0ZXh0L2h0bWwiIGhyZWY9Imh0dHA6Ly93d3cubG9ic3Rlcm1vbnN0ZXIub3JnL3Byb2ZpbGUva29za2kiLz4KICAgIDxsaW5rIHJlbD0iYXZhdGFyIiB0eXBlPSJpbWFnZS9wbmciIGhyZWY9Imh0dHA6Ly93d3cuZ3JhdmF0YXIuY29tL2F2YXRhci9hMGM2ZTYzYjliOGI4ZDRmNmZhYTNjOWVhNjJmNDNiZi5wbmciLz4KICA8L2FjdGl2aXR5OmFjdG9yPgo8L2VudHJ5Pgo=</me:data>
  <me:encoding>base64url</me:encoding>
  <me:alg>RSA-SHA256</me:alg>
  <me:sig>VXBI5WYkJmj82AmdXOsfi3fjn3J7kYJsWsFUGbnGaqntUkA_Sza67eBJDUsoSjhd4Knb-FUb8hTJVzadqaCq_Bj4n0DouoRZ6bW9T1gGS2-Qgpwm_ZVb9xFZogGkHKF6-15_Lb7ntuQcee5tnHwxzMGty51uuai6qiZZ_u51wrk=</me:sig>
</me:env>';
        
        $publicKey = "RSA.quCNBj3KbWmJG1huVxTvHWjCenThHYSb49y7HLPz_fVUfTUYMVfz7Qt8IkTXKj9TartEhNG2FzTIZzu4mkSzkKDZ9NflWs2VIJCWZoF-xJY4FAGKvja-Tuxn-K2trjKa6bypIEfM4qYWVHr_Sxfx3r4fioAe2z90p3AKF6aWm10=.AQAB";
        
        $magicEnv = Magic_Envelope::buildFromRawXML($rawEnvelope);
        
        $verifyier = new Magic_Signature_Verify($publicKey);
        $this->assertEquals(true, $verifyier->magicEnvelopeHasValidSignature($magicEnv));
    }
    
    public function testVerificationOfSignatureSivy() {
        $rawEnvelope = "<?xml version='1.0' ?>
<me:env xmlns:me='http://salmon-protocol.org/ns/magic-env'>
  <me:encoding>base64url</me:encoding>
  <me:data type='application/atom+xml'>
    PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0nVVRGLTgnID8-Cjxl 
     bnRyeSB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwNS9BdG9tJyB4 
     bWxuczphY3Rpdml0eT0iaHR0cDovL2FjdGl2aXR5c3RyZWEubXMvc3Bl 
     Yy8xLjAvIj4KICA8YWN0aXZpdHk6YWN0b3I-CiAgICA8YWN0aXZpdHk6 
     b2JqZWN0LXR5cGU-aHR0cDovL2FjdGl2aXR5c3RyZWEubXMvc2NoZW1h 
     LzEuMC9wZXJzb248L2FjdGl2aXR5Om9iamVjdC10eXBlPgogICAgPGlk 
     Pmh0dHA6Ly9tb25raW5ldGljLnN0YXR1cy5uZXQvdXNlci8xPC9pZD4K 
     CTx0aXRsZT5TdGV2ZSBJdnk8L3RpdGxlPgoJPGxpbmsgcmVsPSJhbHRl 
     cm5hdGUiIHR5cGU9InRleHQvaHRtbCIgaHJlZj0iaHR0cDovL21vbmtp 
     bmV0aWMuc3RhdHVzLm5ldC8iLz4KCTwvYWN0aXZpdHk6YWN0b3I-CiAg 
     ICA8aWQ-dGFnOmV4YW1wbGUuY29tLDIwMTAtMC44OTg2ODg0Mzg4ODc4 
     MDY8L2lkPgogICAgPGF1dGhvcj48bmFtZT5zdGV2ZWl2eUBhdG9sbGlh 
     LmFwcHNwb3QuY29tPC9uYW1lPjx1cmk-c3RldmVpdnlAYXRvbGxpYS5h 
     cHBzcG90LmNvbTwvdXJpPjwvYXV0aG9yPgogICAgPHRocjppbi1yZXBs 
     eS10byB4bWxuczp0aHI9J2h0dHA6Ly9wdXJsLm9yZy9zeW5kaWNhdGlv 
     bi90aHJlYWQvMS4wJwogICAgICAgICByZWY9JzEyODExMzMyNDAnPgoJ 
     MTI4MTEzMzI0MAogICAgPC90aHI6aW4tcmVwbHktdG8-CiAgICA8Y29u 
     dGVudD5TYWxtb24gc3dpbSB1cHN0cmVhbSE8L2NvbnRlbnQ-CiAgICA8 
     dGl0bGU-U2FsbW9uIHN3aW0gdXBzdHJlYW0hPC90aXRsZT4KICAgIDx1 
     cGRhdGVkPjIwMTAtMDgtMDZUMjM6MTk6MzZaPC91cGRhdGVkPgogIDwv 
     ZW50cnk-Cg
  </me:data>
  <me:alg>RSA-SHA256</me:alg>
  <me:sig>
    HmX8BYf_V-dTF58OItH7vuQXFXvqYYc5RRKT-VNp-xSMoiF-8JOrvaMc 
     wxDqml7j99yGeg2xn7I5lSGfGgjz126X5C6dZ5-vrommZCMjJtXEgdoo 
     3CiQBrdj7XqrnsMtjyQkb5CcK3faoLkrM8xZop0Prmhw2lr_RcjNcF8o 
     Y80=
  </me:sig>
</me:env>";
        
        $publicKey = 'RSA.hDq_qUbQAbcU1nY4si3CLS91WGsNuXkB4tor0wHI0pvFYKG81Sr7Y19z_-1RO2blKIxSGHfRo0OFcDNlfRVSmH6Kczk_tVvNzerjeypx-8AN5QiBrZEhi7IU88sJ1wcY0LWHdvboS-WFJuSZmMIdAZRiz7aExQgHzgkuBQfzjRs=.AQAB';
        $magicEnv = Magic_Envelope::buildFromRawXML($rawEnvelope);
        
        $verifyier = new Magic_Signature_Verify($publicKey);
        
        //echo $magicEnv->asXML();
        
        $this->assertEquals(true, $verifyier->magicEnvelopeHasValidSignature($magicEnv));
    }
}